<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> 订单列表 </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-select v-model="orderVo.order.state" placeholder="订单状态" class="handle-select mr10">
                    <el-option key="1" label="待确认" value="1"></el-option>
                    <el-option key="2" label="进行中" value="2"></el-option>
                    <el-option key="3" label="已服务" value="3"></el-option>
                    <el-option key="4" label="已取消" value="4"></el-option>
                    <el-option key="5" label="已完成" value="5"></el-option>
                    <el-option key="n" label="已删除" value="n"></el-option>
                </el-select>
                <el-select v-model="orderVo.order.pwOption" placeholder="陪玩操作" class="handle-select mr10">
                    <el-option key="y" label="接受订单" value="y"></el-option>
                    <el-option key="n" label="拒绝订单" value="n"></el-option>
                    <el-option key="o" label="处理超时" value="o"></el-option>
                    <el-option key="w" label="等待处理" value="w"></el-option>
                </el-select>
                <el-input v-model="orderVo.order.orderNum" placeholder="订单编号" class="handle-input mr10"></el-input>
                <el-input v-model="orderVo.order.user.account" placeholder="用户账号" style="width: 200px"></el-input>
                <br />
                <br />
                <el-input v-model="orderVo.order.minMoney" placeholder="订单的最小金额" style="width: 150px"></el-input>&nbsp;至
                <el-input v-model="orderVo.order.maxMoney" placeholder="订单的最大金额" style="width: 150px"></el-input>&nbsp;
                <el-date-picker v-model="orderVo.order.startTime" type="date" placeholder="选择订单的开始日期"> </el-date-picker>&nbsp;至
                <el-date-picker v-model="orderVo.order.endTime" type="date" placeholder="选择订单的截至日期"> </el-date-picker>&nbsp;
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>&nbsp;
                <el-button type="primary" icon="el-icon-download" @click="handleDownLoad">导出Excel表格</el-button>
            </div>
            <el-table :data="tableData" border class="table" ref="multipleTable" header-cell-class-name="table-header">
                <el-table-column prop="orderNum" label="订单编号"></el-table-column>
                <el-table-column prop="user.account" width="80" label="用户账号"></el-table-column>
                <el-table-column prop="num" width="80" label="下单数量"></el-table-column>
                <el-table-column label="下单金额">
                    <template slot-scope="scope">￥{{ scope.row.money }}</template>
                </el-table-column>
                <el-table-column label="订单状态" align="center">
                    <template slot-scope="scope">
                        <el-tag>
                            <span v-if="scope.row.state == '1'">待确认</span>
                            <span v-if="scope.row.state == '2'">进行中</span>
                            <span v-if="scope.row.state == '3'">已服务</span>
                            <span v-if="scope.row.state == '4'">已取消</span>
                            <span v-if="scope.row.state == '5'">已完成</span>
                            <span v-if="scope.row.state == 'n'">已删除</span>
                        </el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="陪玩操作" align="center">
                    <template slot-scope="scope">
                        <el-tag>
                            <span v-if="scope.row.pwOption == 'y'">接受订单</span>
                            <span v-if="scope.row.pwOption == 'n'">拒绝订单</span>
                            <span v-if="scope.row.pwOption == 'o'">处理超时</span>
                            <span v-if="scope.row.pwOption == 'w'">等待处理</span>
                        </el-tag>
                    </template>
                </el-table-column>
                <el-table-column prop="buyTime" width="200" label="下单时间"></el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button type="text" icon="el-icon-document" @click="handleEdit(scope.$index, scope.row, scope.row.id)"
                            >查看详情</el-button
                        >
                    </template>
                </el-table-column>
            </el-table>

            <div class="pagination">
                <el-pagination
                    background
                    layout="prev, pager, next"
                    :total="totalPages"
                    :current-page="currentPage"
                    @current-change="handlePageChange($event)"
                >
                </el-pagination>
            </div>
        </div>

        <!-- 订单详情弹出框 -->
        <el-dialog title="订单详情" :visible.sync="editVisible" width="30%">
            <el-descriptions direction="vertical" :column="4" border>
                <el-descriptions-item label="用户账号">{{ orderDetailsVo.order.user.account }}</el-descriptions-item>
                <el-descriptions-item label="订单编号">{{ orderDetailsVo.order.orderNum }}</el-descriptions-item>
                <el-descriptions-item label="订单状态">
                    <el-tag size="small"
                        ><span v-if="orderDetailsVo.order.state == '1'">待确认</span>
                        <span v-if="orderDetailsVo.order.state == '2'">进行中</span>
                        <span v-if="orderDetailsVo.order.state == '3'">已服务</span>
                        <span v-if="orderDetailsVo.order.state == '4'">已取消</span>
                        <span v-if="orderDetailsVo.order.state == '5'">已完成</span>
                        <span v-if="orderDetailsVo.order.state == 'n'">已删除</span></el-tag
                    >
                </el-descriptions-item>
                <el-descriptions-item label="陪玩操作">
                    <el-tag size="small">
                        <span v-if="orderDetailsVo.order.pwOption == 'y'">接受订单</span>
                        <span v-if="orderDetailsVo.order.pwOption == 'n'">拒绝订单</span>
                        <span v-if="orderDetailsVo.order.pwOption == 'o'">处理超时</span>
                        <span v-if="orderDetailsVo.order.pwOption == 'w'">等待处理</span>
                    </el-tag>
                </el-descriptions-item>
                <el-descriptions-item label="所属类别">{{ orderDetailsVo.goods.goodsCategory.name }}</el-descriptions-item>
                <el-descriptions-item label="商品描述">{{ orderDetailsVo.goods.description }}</el-descriptions-item>
                <el-descriptions-item label="商品单价">￥{{ orderDetailsVo.goods.money }}</el-descriptions-item>
                <el-descriptions-item label="出售数量">{{ orderDetailsVo.goods.saleNum }} 件</el-descriptions-item>
            </el-descriptions>
        </el-dialog>
    </div>
</template>

<script>
import { fetchData } from '../../api/index';
export default {
    name: 'basetable',
    data() {
        return {
            orderVo: {
                order: {
                    state: null,
                    pwOption: null,
                    orderNum: null,
                    minMoney: null,
                    minMoney: null,
                    startTime: null,
                    endTime: null,
                    user: {
                        account: null
                    }
                },
                page: 1
            },
            query: {
                address: '',
                name: '',
                pageIndex: 1,
                pageSize: 10
            },
            tableData: [],
            orderDetailsVo: {
                order: {
                    orderNum: '',
                    state: '',
                    pwOption: '',
                    user: {
                        account: ''
                    }
                },
                goods: {
                    goodsCategory: {
                        name: ''
                    },
                    description: '',
                    money: '',
                    saleNum: ''
                }
            },
            totalPages: null,
            currentPage: null,
            multipleSelection: [],
            delList: [],
            editVisible: false,
            pageTotal: 0,
            form: {},
            idx: -1,
            id: -1
        };
    },
    created() {
        this.getList();
    },
    methods: {
        //初始化列表
        getList() {
            this.$axios.post('/admin/order/list', this.orderVo).then((r) => {
                if (r.data.code == 200) {
                    this.tableData = r.data.data.data;
                    this.totalPages = r.data.data.totalPages * 10;
                    this.currentPage = r.data.data.currPage;
                } else if (r.data.code == 403) {
                }
            });
        },
        // 触发搜索按钮
        handleSearch() {
            console.log(this.currentPage);
            this.orderVo.page = this.currentPage;
            this.getList();
        },
        handleDownLoad() {
            this.$axios({
                url: '/admin/order/exportFile',
                method: 'post',
                responseType: 'blob' //这里注意一定要把返回值类型改为blob
            }).then(function (r) {
                console.log(r);
                let blob = new Blob([r.data], {
                    type: 'application/vnd.ms-excel'
                });
                console.log(blob);
                let fileName = '订单表.xlsx';
                if (window.navigator.msSaveOrOpenBlob) {
                    // console.log(2)
                    navigator.msSaveBlob(blob, fileName);
                } else {
                    // console.log(3)
                    var link = document.createElement('a');
                    link.href = window.URL.createObjectURL(blob);
                    link.download = fileName;
                    link.click();
                    //释放内存
                    window.URL.revokeObjectURL(link.href);
                }
            });
        },
        // 订单详情
        handleEdit(index, row, id) {
            this.$axios.get('/order/details/' + id).then((r) => {
                if (r.data.code == 200) {
                    this.orderDetailsVo = r.data.data;
                } else if (r.data.code == 403) {
                }
            });
            this.idx = index;
            this.form = row;
            this.editVisible = true;
        },
        // 分页导航
        handlePageChange(currentPage) {
            this.orderVo.page = currentPage;
            this.getList();
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
